package com.zzc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzc.common.JsonResult;
import com.zzc.common.TableResult;
import com.zzc.mapper.RechargeMapper;
import com.zzc.pojo.Recharge;
import com.zzc.service.RechargeService;
import com.zzc.util.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhouzhicheng
 * @version 1.0  2020/12/30
 */
@Service
public class RechargeServiceImpl implements RechargeService {
    @Autowired
    private RechargeMapper rechargeMapper;

    @Override
    public TableResult getRecharge(Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<Recharge> list = rechargeMapper.getAllRecharge();
        PageInfo pageInfo = new PageInfo(list);
        return new TableResult(0,null,Long.valueOf(pageInfo.getTotal()).intValue(),pageInfo.getList());
    }

    @Override
    public JsonResult addRecharge(Recharge recharge) {
        if (recharge==null){
            return JsonResult.getResultFailed("参数传递有误！");
        }
        //订单创建时间
        recharge.setCreatetime(new Date());
        //订单修改时间
        recharge.setUpdatetime(new Date());
        //设置订单号
        recharge.setOrderId(Long.valueOf(RandomUtils.createOrderId()));
        return rechargeMapper.insertSelective(recharge)>0
                ? JsonResult.getResultSuccess("添加成功！")
                : JsonResult.getResultFailed("添加失败！请重试！");
    }

    @Override
    public JsonResult delRecharge(Integer[] ids) {
        if (ids == null) {
            JsonResult.getResultFailed("参数有误！请重试！");
        }
        return rechargeMapper.deleteById(ids)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("删除失败！服务器繁忙！");
    }

    @Override
    public JsonResult updateRecharge(Recharge recharge) {
        if (recharge==null){
            return JsonResult.getResultFailed("参数有误！请重试！");
        }
        recharge.setUpdatetime(new Date());
        return rechargeMapper.updateByPrimaryKeySelective(recharge)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("修改失败!");
    }
}
